package com.food.platformmanagement.domain;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * 值对象
 * 密码应不少于6位，不大于16位
 * 仅由数字与字母组成
 * @author 24270
 */
@Embeddable
public class UserPassword {

    @Column(name = "password")
    private String password;

    public UserPassword(String password) {
        this.password = password;
    }

    public UserPassword() {

    }

    /**
     * TODO 没写完，不是很懂这个逻辑在哪里会用到
     * @param password
     */
    public boolean checkLegal(String password) {
        if(password.length() < 6) {
            throw new UserPasswordException("密码长度不能小于6位");
        }
        if(!password.matches("\\w+")) {
            throw new UserPasswordException("密码必须由数字、字母、下划线组成");
        }
        if(password.length() > 16) {
            throw new UserPasswordException("密码长度不能超过16位");
        }
        return true;
    }

    public String value() {
        return this.password;
    }

    public void change(String password) {
        this.password = password;
    }

}
